#include <QGraphicsSceneMouseEvent>
#include <QDebug>
#include "propcoordinatedialogscene.h"

PropCoordinateDialogScene::PropCoordinateDialogScene(QObject *parent) :
    QGraphicsScene(parent)
{
    prev = QPointF(-999, -999);
}

void PropCoordinateDialogScene::setMapImagePixmap(QPixmap *m)
{
    mapImage = m;
    addPixmap(*mapImage);
}

void PropCoordinateDialogScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    setPoint(event->scenePos());
    if (prev.x() != -999)
    {
        removeItem(itemAt(prev));
    }
    prev = event->scenePos();
}

void PropCoordinateDialogScene::setPoint(QPointF pt)
{
    if (pt.x() < 0 || pt.x() > mapImage->width() ||
            pt.y() < 0 || pt.y() > mapImage->height())
    {
        return;
    }
    emit mouseClickCoords(pt.x(), pt.y());
    QPen pen(Qt::red);
    addRect(pt.x()-2, pt.y()-2, 4, 4, pen);
}
